from utils import ListNode


class Solution:
    def __init__(self):
        self.carry = 0
        self.ans = ListNode(None)
        self.node = self.ans

    def handle_sum(self, two_sum):
        if two_sum > 9:
            two_sum %= 10
            self.carry = 1
        else:
            self.carry = 0
        new_node = ListNode(two_sum)
        self.node.next = new_node
        self.node = self.node.next

    def mysum(self, node1, node2):
        while node1 or node2:
            if not node1:
                two_sum = node2.val + self.carry
                node2 = node2.next
            elif not node2:
                two_sum = node1.val + self.carry
                node1 = node1.next
            else:
                two_sum = node1.val + node2.val + self.carry
                node1 = node1.next
                node2 = node2.next
            self.handle_sum(two_sum)
        if self.carry:
            self.node.next = ListNode(1)
        return self.ans


from utils import list12345, print_list, list95, list999, list1

print_list(list12345)
print_list(list95)

solution = Solution()
solution.mysum(list12345, list95)

print_list(solution.ans.next)
